import 'dart:io';

import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/provide/cmd_provider.dart';
import 'package:full_display/provide/drawer_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_util1.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/socket/udp/socket_udp_utils.dart';
import 'package:full_display/unit/web_socket/web_sockect_utils.dart';
import 'package:full_display/unit/widgets/button/button_addSub_widget.dart';
import 'package:full_display/unit/widgets/button/button_text_widget.dart';
import 'package:full_display/unit/widgets/button/button_widget.dart';
import 'package:full_display/unit/widgets/slider/slider_widget.dart';
import 'package:provider/provider.dart';

/// 抽屉View
class MontageDrawerWidget extends StatelessWidget {
  MontageDrawerWidget({Key? key}) : super(key: key);

  late MontageProvider _montageProvider;
  @override
  void dispose() {}

  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Drawer(
      backgroundColor: ColorPlate.background,
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 60.0.H,
            ),
            GridView.builder(
              padding: EdgeInsets.zero,
              scrollDirection: Axis.vertical,
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: _montageProvider.drawerList.length,
              itemBuilder: (con, index) {
                return ButtonWidget(
                  title: _montageProvider.drawerList[index]['title'],
                  imgName: _montageProvider.drawerList[index]['img'],
                  index: index,
                  iconWidth: 32.0.W,
                  iconHeight: 32.0.W,
                  tapAction: stateClick,
                );
              },
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 12.0.W,
                //宽高比
                childAspectRatio: 1.5,
              ),
            ),
            const SliderWidget(
              title: '亮度调节',
              state: 'udp_light',
              minValue: 0.0,
              maxValue: 15.0,
              sliderValue: 0,
            ),
            Container(
              padding: EdgeInsets.only(left: 20.0.W, top: 40.0.H),
              child: Text(
                '播放模式',
                textAlign: TextAlign.left,
                style: StandardTextStyle.textWhite12Style,
              ),
            ),
            Container(
              height: 10.0.H,
            ),
            GridView.builder(
              padding: EdgeInsets.only(left: 20.0.W),
              scrollDirection: Axis.vertical,
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: _montageProvider.playModelList.length,
              itemBuilder: (con, index) {
                return ButtonTextWidget(
                  title: _montageProvider.playModelList[index]['title'],
                  state: _montageProvider.playModelList[index]['isSelect'],
                  width: 60.0.W,
                  height: 24.0.W,
                  index: index,
                  tapAction: playClick,
                );
              },
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 12.0.W,
                //宽高比
                childAspectRatio: 2 / 1,
              ),
            ),
          ],
        ),
      ),
    );
  }

  //按钮点击
  void stateClick(int index) {
    switch (index) {
      case 0:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:POWERON");
        break;
      case 1:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:POWEROFF");
        break;
      case 2:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:REBOOT");
        break;
      case 3:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:AGING");
        break;
      case 4:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:EXITAGING");
        break;
      case 5:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:TESTLED");
        break;
      case 6:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:EXITTESTLED");
        break;
      case 7:
        SocketUDPUtils()
            .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:SHOWPOS");
        break;

      default:
    }
  }

  //播放模式点击
  void playClick(int index) {
    _montageProvider.changePlayState(index);
    String mode = "";
    switch (index) {
      case 0:
        mode = "LOOP";
        break;
      case 1:
        mode = "RANDOM";
        break;
      case 2:
        mode = "SINGLE";
        break;
      default:
    }
    SocketUDPUtils()
        .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "SET:PLAYMODE:$mode");
  }
}
